package com.ruoyi.ruoyi.business.service.smo.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.pojo.entity.Category;
import com.ruoyi.common.pojo.service.CategoryService;
import com.ruoyi.ruoyi.business.pojo.dto.*;
import com.ruoyi.ruoyi.business.pojo.vo.ClassifyDetailVo;
import com.ruoyi.ruoyi.business.pojo.vo.ClassifyListVo;
import com.ruoyi.ruoyi.business.service.smo.ClassifyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * ClassName：ClassifyServiceImpl
 * Package：com.ruoyi.ruoyi.business.service.smo.impl
 * Description：
 *
 * @Author：fpy
 * @Create：2025/5/7 - 16:56
 * @Version：v1.0
 */
@Service
public class ClassifyServiceImpl implements ClassifyService {
    @Resource
    private CategoryService categoryService;

    @Override
    public AjaxResult<Page<ClassifyListVo>> list(Page page, ClassifyListDto dto) {
        Page<Category> categoryPage = categoryService.page(page,
                Wrappers.<Category>lambdaQuery()
                        .like(StrUtil.isNotBlank(dto.getName()), Category::getName, dto.getName())
                        .eq(StrUtil.isNotBlank(dto.getShow_flag()), Category::getShowFlag, dto.getShow_flag())
                        .orderByAsc(Category::getSort, Category::getUpdateTime)
        );

        Page<ClassifyListVo> pageVo = new Page<>();
        pageVo.setTotal(categoryPage.getTotal());
        pageVo.setSize(categoryPage.getSize());
        pageVo.setCurrent(categoryPage.getCurrent());
        pageVo.setRecords(
                Optional.ofNullable(categoryPage.getRecords())
                        .orElseGet(ArrayList::new)
                        .stream()
                        .map(c -> {
                            ClassifyListVo vo = new ClassifyListVo();
                            vo.setId(c.getId());
                            vo.setName(c.getName());
                            vo.setIcon(c.getIcon());
                            vo.setSort(c.getSort());
                            vo.setShow_flag(c.getShowFlag());
                            vo.setUpdate_time(c.getUpdateTime());

                            return vo;
                        })
                        .collect(Collectors.toList())
        );

        return AjaxResult.success(pageVo);
    }

    @Override
    public AjaxResult add(ClassifyAddDto dto) {
        Category category = new Category();
        category.setName(dto.getName());
        category.setIcon(dto.getIcon());
        category.setSort(dto.getSort());
        category.setShowFlag(dto.getShow_flag());
        categoryService.save(category);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult remove(ClassifyRemoveDto dto) {
        categoryService.removeById(dto.getId());

        return AjaxResult.success();
    }

    @Override
    public AjaxResult<ClassifyDetailVo> detail(ClassifyDetailDto dto) {
        Category category = categoryService.getById(dto.getId());
        if (category == null) {
            return AjaxResult.error("分类不存在");
        }

        ClassifyDetailVo vo = new ClassifyDetailVo();
        vo.setId(category.getId());
        vo.setName(category.getName());
        vo.setIcon(category.getIcon());
        vo.setSort(category.getSort());
        vo.setShow_flag(category.getShowFlag());

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult edit(ClassifyEditDto dto) {
        Category category = new Category();
        category.setId(dto.getId());
        category.setName(dto.getName());
        category.setIcon(dto.getIcon());
        category.setSort(dto.getSort());
        category.setShowFlag(dto.getShow_flag());
        categoryService.updateById(category);

        return AjaxResult.success();
    }
}
